import unittest
from common.open_browser import open_browser
from selenium.webdriver.support.ui import Select
from page.buy_shop_car_page import Buyshop
from page.login_page import LoginPage
from common.base import Base, By


# 点击购物车按钮能成功跳转购物车页面
# 添加商品到购物车, 预期: 成功添加,购物车有商品
# 修改购物车商品数量
# 删除购物车指定商品
# 清空购物车所有商品
# 购物车成功结算,订单提交成功

class ShopCar(unittest.TestCase):
    driver = open_browser()

    @classmethod
    def setUpClass(cls) -> None:
        # 打开浏览器

        # 输入网址
        cls.website_addr = Buyshop(cls.driver)
        cls.website_addr.get('http://172.16.140.47/ecshop/user.php')
        cls.website_addr.login_ecshop()

    # 点击购物车按钮能成功跳转购物车页面
    def test1_car(self):
        # 定位购物车元素
        self.website_addr.car_shop()
        # 跳转验证
        shop_name = self.website_addr.get_element_text(('xpath', '//*[@id="formCart"]/table[1]/tbody/tr/th[1]'))
        # 断言: shop_name = 商品名称, 测试通过
        self.assertEqual(shop_name, '商品名称')

    # 点击商品购买成功添加商品
    def test2_insert_shop(self):
        # 定位商品,点击购买,加入购物车
        self.website_addr.find_shop()
        # 成功添加验证
        phone_shop = self.website_addr.get_element_text(
            ('xpath', '//*[@id="formCart"]/table[1]/tbody/tr[2]/td[1]/a[2]'))
        # 断言:phone_shop = 智能相机,测试通过
        self.assertEqual(phone_shop, '智能相机')

    # 修改购物车商品数量
    def test3_update_shopnum(self):
        # 定位商品数量input框,修改数量
        self.website_addr.update_shop()
        # 成功修改商品数量

        shop_num = self.website_addr.get_attribute(('class name', "inputBg"), 'value')
        # 断言:int(shop_num) = 2,测试通过
        self.assertEqual(int(shop_num), 2)

    # 删除购物车指定商品
    def test4_delete_shop(self):
        self.website_addr.goto_buy()
        # 定位商品删除元素,点击删除
        self.website_addr.delete_shop()
        shopn = self.website_addr.get_attribute(('link text', '运动相机'), 'outerHTML')
        print(shopn)
        self.assertEqual(shopn, None)

    # 提交订单
    def test5_pay_money(self):
        # 定位结算按钮元素,点击
        self.website_addr.buy()
        # 成功提交订单
        self.website_addr.delivery_way()
        all_shop_car = self.website_addr.get_element_text(('xpath', '//*[@id="ECS_CARTINFO"]/a'))
        # 断言:all_shop_car = 购物车(0),断言通过
        self.assertEqual(all_shop_car, '购物车(0)')

    # 清空购物车
    def test6_shop_car(self):
        self.website_addr.get('http://172.16.140.47/ecshop/user.php')
        self.website_addr.find_shop()
        # 定位清空购物车元素,点击
        self.website_addr.delete_car()
        # 成功删除购物车所有商品
        shop_car = self.website_addr.get_element_text(('xpath', '//*[@id="ECS_CARTINFO"]/a'))
        # 断言: shop_car = 购物车(0),断言通过
        self.assertEqual(shop_car, '购物车(0)')

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)
